<?php

/**
 * 商品详情
 */

namespace app\mall\api;

use \app\base\api\BaseApi;

class InfoApi extends BaseApi {

    protected $info;
    protected $id;

    public function __construct() {
        parent::__construct();
        $id = $this->data['id'];

        if(empty($id)){
            $this->error404();
        }
        $info = target('mall/Mall')->getInfo($id);
        if(empty($info) || !$info['status']) {
            $this->error404();
        }
        $info['images'] = unserialize($info['images']);
        $this->id = $id;
        $this->info = $info;
    }


    /**
     * 详情
     */
    public function index() {

        $info = $this->info;
        $id = $this->id;

        $commentList = target('order/OrderComment')->loadList([
            'A.app' => 'mall',
            'A.has_id' => $id
        ], 20);

        $mallData = target('mall/Info', 'service')->getData($id, 0, $info);


        $proInfo = $mallData['proInfo'];

        if(empty($proInfo)) {
            $this->error404();
        }


        $this->success('ok', [
            'info' => $info,
            'proInfo' => $proInfo,
            'skuList' => $mallData['skuList'],
            'specList' => $mallData['specList'],
            'comment' => $commentList,
            'share' => $this->share()
        ]);
    }

    private function share() {

        $info = $this->info;
        $id = $this->id;

        $saleCode = '';

        //获取登录信息
        $userId = $_SERVER['HTTP_AUTHUID'];
        if($userId) {
            if($info) {
                $saleUser = target('sale/SaleUser')->getWhereInfo([
                    'A.user_id' => $userId,
                    'agent' => 1
                ]);
                if($saleUser) {
                    $saleCode = $saleUser['code'];
                }
            }
        }

        return [
            'title' => $info['title'],
            'desc' => $info['description'],
            'image' => $info['image'],
            'url' => url('controller/mall/Info/index', ['id' => $id, 'sale_code' => $saleCode], true)
        ];

    }

}